package com.sc.nft.admin.controller;

import com.sc.nft.admin.request.IdRequest;
import com.sc.nft.annotation.SysMemberLogin;
import com.sc.nft.entity.dto.WantBuyOrderAdminPageDTO;
import com.sc.nft.entity.dto.WantBuyOrderAdminRecordDTO;
import com.sc.nft.entity.vo.WantBuyOrderAdminRecordVO;
import com.sc.nft.entity.vo.WantBuyOrderVO;
import com.sc.nft.enums.RedisKeyEnum;
import com.sc.nft.exception.GlobalException;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.service.WantBuyOrderService;
import com.sc.nft.sup.PageResult;
import com.sc.nft.sup.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.concurrent.TimeUnit;

/**
 * @author 刘国航
 * @desc
 * @since 2024/12/19 20:08
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/admin/wantBuyOrder")
@Api(tags = "求购单的后台")
public class WantBuyOrderController {

    private final WantBuyOrderService wantBuyOrderService;
    private final RedissonClient redissonClient;

    @PostMapping(value = "/page", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "求购单分页列表", notes = "求购单分页列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public PageResult<WantBuyOrderVO> page(@Valid @RequestBody WantBuyOrderAdminPageDTO dto) {
        return PageResult.success(wantBuyOrderService.page(dto.toMp(), dto.getUserTel(), dto.getStatus()));
    }

    @PostMapping(value = "/recordPage", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "求购单成交记录", notes = "求购单成交记录", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public PageResult<WantBuyOrderAdminRecordVO> recordPage(@Valid @RequestBody WantBuyOrderAdminRecordDTO dto) {
        return PageResult.success(wantBuyOrderService.recordPage(dto.toMp(), dto.getId(), dto.getSellUserTel(), dto.getStartCreateTime(), dto.getEndCreateTime(), dto.getStartPayTime(), dto.getEndPayTime()));
    }

    @PostMapping(value = "/close", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "关闭求购单", notes = "关闭求购单", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result close(@Valid @RequestBody IdRequest request) {
        RLock lock = redissonClient.getLock(RedisKeyEnum.WANT_BUY_ORDER.getKey() + request.getId());
        try {
            boolean res = lock.tryLock(30, 30, TimeUnit.SECONDS);
            if (!res) {
                throw new GlobalException("操作频繁，请重试！！！！");
            }
            wantBuyOrderService.close(request.getId());
        } catch (Exception e) {
            throw new GlobalRunTimeException(e.getMessage());
        } finally {
            if (lock.isLocked() && lock.isHeldByCurrentThread()) {
                lock.unlock();
            }
        }
        return Result.success();
    }

}
